package com.crack.nga2.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.crack.nga2.R;
import com.crack.nga2.custom.PullListView;
import com.crack.nga2.entity.SubForum;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SubForumFragment extends Fragment implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, PullListView.OnRefreshListener {

    private PullListView listView;
    private View rootView;
    private ForumListAdapter adapter = new ForumListAdapter();
    private List<SubForum> list = new ArrayList<>();
    private int index;

    public SubForumFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_sub_forum, container, false);
        listView = (PullListView) rootView.findViewById(R.id.listView);
        //禁用上拉加载
        listView.setNoPullUp(true);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
        listView.setOnRefreshListener(this);
        return rootView;
    }


    public void removeForum(SubForum forum) {
        list.remove(forum);
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("onResume",String.valueOf(index));
    }

    public void addForumList(List<SubForum> list) {
        if (list == null) {
            return;
        }
        this.list.addAll(list);
        adapter.notifyDataSetChanged();
        if (listView != null) {
            listView.onCompleteRefresh();
        }
    }

    public void addForum(SubForum forum) {
        list.remove(forum);
        list.add(forum);
        adapter.notifyDataSetChanged();
    }

    public boolean contains(SubForum forum) {
        return list.contains(forum);
    }

    public void setIndex(int index) {
        this.index = index;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /*
        * 点击item事件交由activity处理 activity必须实现onClickForumListener接口
        */
        onClickForumListener listener = (onClickForumListener) getActivity();
        listener.clickForum(parent, view, position, id);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        /*
        * 长按item事件交由activity处理 activity必须实现onLongClickForumListener接口
        */
        onLongClickForumListener listener = (onLongClickForumListener) getActivity();
        return listener.clickLongForum(parent, view, position, id);
    }

    @Override
    public void pullDownToRefresh() {
        onDownRefreshListener listener = (onDownRefreshListener) getActivity();
        listener.pullDownToRefresh(index, this);
    }

    @Override
    public void pullUpToRefresh() {

    }

    public interface onDownRefreshListener {
        void pullDownToRefresh(int index, SubForumFragment fragment);
    }

    public interface onClickForumListener {

        void clickForum(AdapterView<?> parent, View view, int position, long id);
    }

    public interface onLongClickForumListener {

        boolean clickLongForum(AdapterView<?> parent, View view, int position, long id);
    }

    private class ForumListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return list.get(position).getFid();
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder holder;
            if (view == null) {
                view = View.inflate(getContext(), R.layout.fragment_sub_forum_item, null);
                holder = new ViewHolder();
                holder.tvName = (TextView) view.findViewById(R.id.tv_name);
                holder.tvSurName = (TextView) view.findViewById(R.id.tv_surname);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            SubForum forum = list.get(position);
            holder.tvName.setText(forum.getName());
            if (forum.getInfo() == null) {
                holder.tvSurName.setVisibility(View.GONE);
            } else {
                holder.tvSurName.setText(forum.getInfo());
            }
            return view;
        }

        private class ViewHolder {
            TextView tvName;
            TextView tvSurName;
        }
    }

}
